package com.ziqikeji.xianyan.service.user;
;
import com.ziqikeji.xianyan.common.BaseService;
import com.ziqikeji.xianyan.entity.user.Role;
import com.ziqikeji.xianyan.entity.user.User;
import com.ziqikeji.xianyan.enumeration.DeleteFlagEnum;
import com.ziqikeji.xianyan.repository.user.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户 service
 *
 * @author liuyu
 */
@Service
public class UserService extends BaseService<User, UserDao> {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleService roleService;

    public List<User> getAllByDeleteFlag() {
        return this.userDao.findUserByDeleteFlagEnum(DeleteFlagEnum.NO_DELETE);
    }

    public User getByLoginName(String name) {
        return this.userDao.findUserByLoginName(name);
    }


    /**
     * 增加用户角色
     *
     * @param roleIdsStr 角色id
     * @param user    需要增加角色的用户
     * @return 用户
     */
    public User addRoleByRoleId(String roleIdsStr, User user) {
        String[] roleIdArr = roleIdsStr.split(",");
        if (roleIdArr.length > 0) {
            for (String roleId : roleIdArr) {
                Role role = roleService.get(roleId);
                List<Role> roleList = user.getRoleList();
                if (!roleList.contains(role)) {
                    roleList.add(role);
                    user.setRoleList(roleList);
                    user.setRoleIdStr((null != user.getRoleIdStr() ? user.getRoleIdStr() : "") + roleId + ",");
                }
            }
        }
        return user;
    }

    /**
     * 设置新的用户角色
     *
     * @param roleIdsStr role 的角色ID数组
     * @param user    用户
     * @return 用户
     */
    public User setRoleByRoleId(String roleIdsStr, User user) {
        String[] roleIdArr = roleIdsStr.split(",");
        List<Role> roleList = new ArrayList<>();
        if (roleIdArr.length > 0) {
            for (String roleId : roleIdArr) {
                Role role = roleService.get(roleId);
                roleList.add(role);
            }
            user.setRoleList(roleList);
        }
        user.setRoleIdStr(roleIdsStr);
        return user;
    }

    /**
     * 删除用户角色
     *
     * @param roleId  角色id
     * @param user 需要增加删除的用户
     * @return 用户
     */
    public User deleteRoleByRoleId(String roleId, User user) {
        Role role = roleService.get(roleId);
        List<Role> roleList = user.getRoleList();
        if (!roleList.contains(role)) {
            roleList.remove(role);
            user.setRoleList(roleList);
        }
        return user;
    }

    /**
     * 用于用户注册时，用户的保存
     *
     * @param email  注册邮箱
     *
     * @param password  密码
     * @return 用户实体
     */
    public User addUser(String email, String password) {
        User user = new User();
        //设置注册信息
        user.setEmail(email);
        //默认使用邮箱做用户名
        user.setLoginName(email);
        user.setPassword(password);
        //设置用户权限 为用户
        Role role = this.roleService.getByName("ROLE_USER");
        List<Role> roleList = user.getRoleList();
        roleList.add(role);
        user.setRoleList(roleList);
        user.setRoleIdStr(role.getId() + ",");
        return this.save(user);
    }
}
